﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using DataModel;
using BasicDataManagement;
using CashierCashFlowManagement;

namespace SystemWindow
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        SystemUser user;
        IOpertation op;

        public bool canClose;

        public MainWindow()
        {
            InitializeComponent();
            op = OperationFactory.Create();
            user = new SystemUser()
            {
                StaffID = 1000,
                StaffName = "admin"
            };
            canClose = false;
        }

        public MainWindow(SystemUser user):this()
        {
            logset(user);
        }

        public void logset(SystemUser user)
        {
            this.user = user;
            btnCashier.Visibility = user.Group.CashierPermission ? Visibility.Visible : Visibility.Collapsed;
            btnData.Visibility = user.Group.DataManagePermission ? Visibility.Visible : Visibility.Collapsed;
            btnStock.Visibility = user.Group.StockManagePermission ? Visibility.Visible : Visibility.Collapsed;
            btnStaff.Visibility = user.Group.StaffManagePermission ? Visibility.Visible : Visibility.Collapsed;
            btnPreproduce.Visibility = user.Group.DataManagePermission ? Visibility.Visible : Visibility.Collapsed;
            //btnMoney.IsEnabled = user.Group.MoneyManagePermission ? true : false;
            btnLogout.Visibility = user.Group.ReliefPermission ? Visibility.Visible : Visibility.Collapsed;
            btnCashflow.Visibility = user.Group.CashFlowPermission ? Visibility.Visible : Visibility.Collapsed;
            btnCheckStock.Visibility = user.Group.CheckStockPermission ? Visibility.Visible : Visibility.Collapsed;
            btnPurchase.Visibility = user.Group.PurchasePermission ? Visibility.Visible : Visibility.Collapsed;
            btnReimburse.Visibility = user.Group.ReimbursementPermission ? Visibility.Visible : Visibility.Collapsed;
            
            this.Title = "您好，" + user.StaffName;
        }

        private void ButtonCashier_Click(object sender, RoutedEventArgs e)
        {
            Cashier.OrderWindow cashier = new Cashier.OrderWindow(user);
            cashier.WindowStartupLocation = WindowStartupLocation.CenterScreen;
            cashier.WindowState = WindowState.Maximized;
            cashier.Owner = this;
            this.Hide();
            cashier.ShowDialog();
            //this.Hide();
        }

        private void ButtonData_Click(object sender, RoutedEventArgs e)
        {
            BasicDataManagement.DataMngWindow win = new BasicDataManagement.DataMngWindow();
            win.WindowStartupLocation = WindowStartupLocation.CenterScreen;
            win.WindowState = WindowState.Maximized;
            win.ShowDialog();
            //this.Hide();
        }

        private void ButtonStock_Click(object sender, RoutedEventArgs e)
        {
            StockManagement.MainWindow win = new StockManagement.MainWindow(user);
            win.WindowStartupLocation = WindowStartupLocation.CenterScreen;
            win.WindowState = WindowState.Maximized;
            win.ShowDialog();
        }

        private void ButtonStaff_Click(object sender, RoutedEventArgs e)
        {
            StaffManagement.MainWindow form = new StaffManagement.MainWindow(user);
            form.WindowStartupLocation = WindowStartupLocation.CenterScreen;
            form.ShowDialog();
        }

        private void ButtonLogout_Click(object sender, RoutedEventArgs e)
        {
            //MessageBox.Show("此班交易发生额：" + op.LoadStaffLogPrice(user.LogID).ToString());
            DutyChangeWindow dutyWin = new DutyChangeWindow(user);
            dutyWin.Owner = this;
            dutyWin.WindowStartupLocation = WindowStartupLocation.CenterOwner;
            if (dutyWin.ShowDialog() == true)
            {
                //if (true)//下一班登陆成功
                //{
                //    //op.Logout(user);
                //    //(this.Parent as Window).Show();
                //    //this.Close();
                //}
            }
        }

        private void ButtonPreproduce_Click(object sender, RoutedEventArgs e)
        {
            Preproduce.MainWindow win = new Preproduce.MainWindow();
            win.WindowStartupLocation = WindowStartupLocation.CenterScreen;
            //win.WindowState = WindowState.Maximized;
            win.ShowDialog();
        }

        private void ButtonMoney_Click(object sender, RoutedEventArgs e)
        {
            MoneyChangeWindow win = new MoneyChangeWindow(user);
            win.Owner = this;
            win.WindowStartupLocation = WindowStartupLocation.CenterOwner;
            win.ShowDialog();
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            //if (canClose == false)
            //{
            //    e.Cancel = true;
            //    MessageBox.Show("请以“下班”方式退出");
            //}
            //else
            //{
            //    e.Cancel = false;
            //}
        }
        //现金流向
        private void ButtonCashflow_Click(object sender, RoutedEventArgs e)
        {
            CashFlowForm cff = new CashFlowForm(this.user);
            cff.ShowDialog();
        }
        //报销操作
        private void ButtonReimburse_Click(object sender, RoutedEventArgs e)
        {
            ReimbursementForm rf = new ReimbursementForm(user);
            rf.ShowDialog();
        }
        //盘点采购
        private void ButtonCheckStock_Click(object sender, RoutedEventArgs e)
        {
            CheckStockManagement.MainForm mf = new CheckStockManagement.MainForm();
            mf.ShowDialog();
        }
        //采购报销
        private void ButtonPurchase_Click(object sender, RoutedEventArgs e)
        {
            PurchaseForm pf = new PurchaseForm(user);
            pf.ShowDialog();
        }
    }
}
